package com.telit.firecommand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.telit.common.enums.ResultCode;
import com.telit.common.exception.ForestBaseException;
import com.telit.common.util.JwtTokenUtils;
import com.telit.firecommand.dao.CommMsgPushReceiverDao;
import com.telit.firecommand.entity.CommMsgPushReceiver;
import com.telit.firecommand.service.MsgPushReceiverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;


/**
 * @Auther amy
 * @Describe
 */
@Service
public class MsgPushReceiverServiceImpl implements MsgPushReceiverService {

    @Autowired
    private CommMsgPushReceiverDao commMsgPushReceiverDao;

    @Override
    public IPage<CommMsgPushReceiver> listByPage(Integer current, Integer size, String receiverName) {
        IPage<CommMsgPushReceiver> iPage = new Page<>(current, size);
        return commMsgPushReceiverDao.listByPage(iPage,receiverName);
    }

    @Override
    @Transactional
    public int add(CommMsgPushReceiver commMsgPushReceiver) {
        commMsgPushReceiver.setCreateTime(new Date());
        commMsgPushReceiver.setCreateUser(JwtTokenUtils.getRealName());
        // 校验手机号是否重复
        isExistMobile(commMsgPushReceiver);
        int count = commMsgPushReceiverDao.insert(commMsgPushReceiver);
        if( count <= 0){
            throw new ForestBaseException(ResultCode.OPERATE_ERROR.getCode(),"新增失败");
        }

        return count;
    }

    @Override
    @Transactional
    public int update(CommMsgPushReceiver commMsgPushReceiver) {
        commMsgPushReceiver.setUpdateTime(new Date());
        commMsgPushReceiver.setUpdateUser(JwtTokenUtils.getRealName());
        isExistMobile(commMsgPushReceiver);
        int count = commMsgPushReceiverDao.updateById(commMsgPushReceiver);
        if(count <= 0){
            throw new ForestBaseException(ResultCode.OPERATE_ERROR.getCode(),"更新失败");
        }
        return count;
    }

    @Override
    @Transactional
    public int deleteBatch(List<Long> ids) {
        int count = 0;
        if(!CollectionUtils.isEmpty(ids)){
            count = commMsgPushReceiverDao.deleteBatchIds(ids);
        }

        return count;
    }

    @Override
    public List<String> selectMobileListByType() {
        return commMsgPushReceiverDao.selectListByType();
    }

    /**
     * 判断手机号是否重复
     */
    private void isExistMobile(CommMsgPushReceiver commMsgPushReceiver){
        QueryWrapper<CommMsgPushReceiver> wrapper = new QueryWrapper<>();
        wrapper.eq("receiver_mobile",commMsgPushReceiver.getReceiverMobile());
        wrapper.eq("deleted","0");
        if(commMsgPushReceiver.getId() != null){
            wrapper.ne("id",commMsgPushReceiver.getId());
        }
        Integer userCount = commMsgPushReceiverDao.selectCount(wrapper);
        if (userCount > 0) {
            throw new ForestBaseException(ResultCode.OPERATE_ERROR.getCode(),"手机号已存在");
        }
    }

}
